package phytagorasbaum;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;

import com.dinim.graphics.GraphicsContextSwingImpl;
import com.dinim.graphics.IGraphicsContext;
import com.dinim.matrix.Vector;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.object.Point2D;
import com.dinim.object.Rectangle2D;

public class PhytagorasbaumComponent extends JComponent{

    PythagorasBaumData pbd;
    public int pointNr = 0;
    public boolean changePoints = false;
    
	public PhytagorasbaumComponent() {

        
        
		int x = 180;
        int y = 200;
        int width = 50;
        int height = 50;

        pbd = new PythagorasBaumData(x, y, width, height);
        
        
		addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent e) {
                System.out.println(changePoints);
				if(!changePoints){
                    pbd.x = e.getX();
                    pbd.y = e.getY();            
                }else{
                    pbd.root.getPoints()[pointNr].setX(e.getX());
                    pbd.root.getPoints()[pointNr].setY(e.getY());
                }
				repaint();
			}

			public void mouseEntered(MouseEvent arg0) {
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}
			
		});
		
		addMouseMotionListener(new MouseMotionListener(){

			public void mouseDragged(MouseEvent e) {
                System.out.println(changePoints);
                if(!changePoints){
                    pbd.x = e.getX();
                    pbd.y = e.getY();            
                }else{
                    pbd.root.getPoints()[pointNr].setX(e.getX());
                    pbd.root.getPoints()[pointNr].setY(e.getY());
                }
                repaint();
			}

			public void mouseMoved(MouseEvent arg0) {
				
			}
			
		});
        
        
	}
	
	public void paint(Graphics g) {
		
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						    RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.white);
		g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        doBaumAusrufezeichen(pbd.root, g, 4);

	}
	
	private void doBaumAusrufezeichen(Rectangle2D rectangle, Graphics g, int nr){

        IGraphicsContext gc = new GraphicsContextSwingImpl(g);
        rectangle.draw(gc);
        
        IPoint2D[] p = rectangle.getPoints();

        IPoint2D[] alphaPoints = new Point2D[] {new Point2D(p[0]), new Point2D(p[1]), new Point2D(p[2]), new Point2D(p[3])};
        IPoint2D[] betaPoints = new Point2D[] {new Point2D(p[0]), new Point2D(p[1]), new Point2D(p[2]), new Point2D(p[3])};
        
        Rectangle2D alphaRect = new Rectangle2D(alphaPoints, Rectangle2D.createConnections());
        Rectangle2D betaRect = new Rectangle2D(betaPoints, Rectangle2D.createConnections());;
        
        alphaRect.scale(pbd.getAlphaRatio(), pbd.getAlphaRatio(), alphaRect.getCenterPoint());
        betaRect.scale(pbd.getBetaRatio(), pbd.getBetaRatio(), betaRect.getCenterPoint());
        
        alphaRect.translate(3, rectangle.getPoints()[0]);
        betaRect.translate(2, rectangle.getPoints()[1]);
        
        
        alphaRect.rotate(-pbd.getAlpha(), alphaRect.getPoints()[3]);
        betaRect.rotate(pbd.getBeta(), betaRect.getPoints()[2]);
        
        if(nr > 0){
        	doBaumAusrufezeichen(alphaRect, g, nr-1);
        	doBaumAusrufezeichen(betaRect, g, nr-1);
        }

	}
	
}
